package org.example;


import org.neo4j.driver.*;

import static org.neo4j.driver.Values.parameters;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: org.example
 * @Date: 2021/12/2 20:49
 * @Description:
 */

public class HelloWorld implements AutoCloseable {
    private Driver driver = null;

    public HelloWorld(String uri, String user, String password) {
        driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
    }

    @Override
    public void close() throws Exception {
        driver.close();
    }

    public void printGreeting(final String message) {
        try (Session session = driver.session()) {
            String greeting = session.writeTransaction(tx -> {
                Result result = tx.run("CREATE (a:Greeting) SET a.message = $message RETURN a.message + ', from node ' + id(a)",
                        parameters("message", message));
                return result.single().get(0).asString();
            });
            System.out.println(greeting);
        }
    }

    public static void main(String... args) throws Exception {
        try (HelloWorld greeter = new HelloWorld("bolt://localhost:7687", "neo4j", "123456")) {
            greeter.printGreeting("hello, world");
        }
    }
}